//
//  loginView.m
//  Follow Me
//
//  Created by apple on 2017/4/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "loginView.h"
#import "registerView.h"
#import "myInformationView.h"
#import "Users+CoreDataClass.h"

@interface loginView ()<UITextFieldDelegate>


@property (nonatomic,strong)loginView *regis;
@property (nonatomic,strong)myInformationView *myinfo;
//@property (nonatomic) Users *user;
@end




@implementation loginView




- (void)awakeFromNib
{
    [super awakeFromNib];
    self.wrongNoticeLabel.hidden = YES;
    
    
    self.SignInButton.layer.cornerRadius = 20;
    self.SignInButton.layer.borderWidth = 0.7f;
    self.SignInButton.layer.borderColor = [UIColor greenColor].CGColor;

    //change the color of placeholder
    self.UserName.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"User Name" attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    self.UserName.layer.cornerRadius = 20;
    self.UserName.layer.borderWidth = 0.7f;
    self.UserName.layer.borderColor = [UIColor whiteColor].CGColor;
    self.Password.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"Password" attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    self.Password.layer.cornerRadius = 20;
    self.Password.layer.borderWidth = 0.7f;
    self.Password.layer.borderColor = [UIColor whiteColor].CGColor;
    
    self.UserName.delegate = self;
    self.Password.delegate = self;
    
    
}
//点击空白处隐藏键盘objective-c
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.UserName endEditing:YES];
    [self.Password endEditing:YES];
}

//完成输入后return隐藏键盘objective-c
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (IBAction)goToRegister:(id)sender {
    self.regis = [[NSBundle mainBundle] loadNibNamed:@"registerView" owner:self options:nil].firstObject;
    [self addSubview:self.regis];
    
   
}

- (IBAction)successToLogin:(id)sender {
    
    [self.UserName endEditing:YES];
    [self.Password endEditing:YES];
    self.wrongNoticeLabel.alpha = 1.0f;
    
    NSArray *personalName = [Users findByAttribute:@"userName" withValue: self.UserName.text];
    if (personalName.count ==0) {
        self.wrongNoticeLabel.layer.cornerRadius = 10;
        self.wrongNoticeLabel.layer.masksToBounds = YES;
        self.wrongNoticeLabel.hidden = NO;
//        self.wrongNoticeLabel.alpha = 1.0f;
        [UIView animateWithDuration:2.5 animations:^{
            
            self.wrongNoticeLabel.alpha = 0.0f;
            
        }completion:^(BOOL finished) {
            self.wrongNoticeLabel.hidden = YES;
            
        }];
        
        self.UserName.text = @"";
        self.Password.text = @"";
        [self.UserName endEditing:YES];
        [self.Password endEditing:YES];
        return;
    }
    
    
    //如果用户名存在，判断密码是否正确
    Users *user = [personalName firstObject];
    if ([user.password isEqualToString:self.Password.text]) {
        NSLog(@"login success");
        [MainModel model].userMsg = user;
        self.myinfo = [[NSBundle mainBundle]loadNibNamed:@"myInformationView" owner:self options:nil].firstObject;
                [self addSubview:self.myinfo];
    }else{
        NSLog(@"failure");
        self.wrongNoticeLabel.layer.cornerRadius = 10;
        self.wrongNoticeLabel.layer.masksToBounds = YES;
        self.wrongNoticeLabel.hidden = NO;
//        self.wrongNoticeLabel.alpha = 1.0f;
        [UIView animateWithDuration:2.5 animations:^{
            
            self.wrongNoticeLabel.alpha = 0.0f;
            
        }completion:^(BOOL finished) {
            self.wrongNoticeLabel.hidden = YES;
            
        }];
        
        self.UserName.text = @"";
        self.Password.text = @"";
        [self.UserName endEditing:YES];
        [self.Password endEditing:YES];
    }
    
//    NSArray *personalPassword = [Users findByAttribute:@"password" withValue:self.Password.text];
//    
//    
//    NSNumber *count = [Users numberOfEntities];
////    NSPredicate *presicate1 = [NSPredicate predicateWithFormat:@"yunxiao0712",personalName];
////    NSNumber *precount = [Users numberOfEntitiesWithPredicate:presicate1];
//    NSLog(@"%@ %@ %@",personalName,personalPassword,count);
////    NSLog(@"%@",precount);
//    if (personalName.count != 0&&personalPassword.count !=0){
//        self.myinfo = [[NSBundle mainBundle]loadNibNamed:@"myInformationView" owner:self options:nil].firstObject;
//        [self addSubview:self.myinfo];
//    }else{
//        
//    }
    
    
    
    
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
